package com.cat.binaryTree;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/create-binary-tree-from-descriptions/
 * @create 2025/9/28 18:34
 * @since JDK17
 */

public class Solution80 {
    public TreeNode createBinaryTree(int[][] descriptions) {
        HashMap<Integer, TreeNode> map = new HashMap<>();
        Set<Integer> h = new HashSet<>();
        for (var des : descriptions) {
            var cur = map.computeIfAbsent(des[0], i -> new TreeNode(des[0]));
            var son = map.computeIfAbsent(des[1], i -> new TreeNode(des[1]));
            h.add(des[1]);
            if (des[2] == 1) {
                cur.left = son;
            } else {
                cur.right = son;
            }
        }
        for (var ent : map.entrySet()) {
            if (h.contains(ent.getKey()) == false) {
                return ent.getValue();
            }
        }

        return null;
    }
}
